﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using EpiNEXT.Core;
using EpiNEXT.Core.Collections;
using EpiNEXT.Core.Fields;

namespace EpiNEXT.WPF
{
    public class FormBaseViewModel : ObservableObject
    {
        #region Members
        private DataEntryInstrument _form;
        private static object _recordsLock = new object();
        #endregion // Members

        #region Properties
        public DataEntryInstrument Form
        {
            get { return this._form; }
            private set
            {
                this._form = value;
                RaisePropertyChanged("Form");
            }
        }

        public Project Project
        {
            get { return Form.Owner; }
            private set
            {
                Form.Owner = value;
                RaisePropertyChanged("Project");
            }
        }

        public DateTime DateCreated
        {
            get { return Form.DateCreated; }
            private set
            {
                Form.DateCreated = value;
                RaisePropertyChanged("DateCreated");
            }
        }

        public DateTime DateLastSaved
        {
            get { return Form.DateLastSaved; }
            private set
            {
                Form.DateLastSaved = value;
                RaisePropertyChanged("DateLastSaved");
            }
        }

        public long LastSaved
        {
            get { return Form.LastSaved; }
            private set
            {
                Form.LastSaved = value;
                RaisePropertyChanged("LastSaved");
            }
        }

        public long Created
        {
            get { return Form.Created; }
            private set
            {
                Form.Created = value;
                RaisePropertyChanged("Created");
            }
        }

        public Guid FormId
        {
            get { return Form.FormId; }
            private set
            {
                Form.FormId = value;
                RaisePropertyChanged("FormId");
            }
        }

        public string Title
        {
            get { return Form.Title; }
            private set
            {
                Form.Title = value;
                RaisePropertyChanged("Title");
            }
        }

        public FieldCollection Fields
        {
            get { return Form.Fields; }
            //private set
            //{
            //    Form.Fields = value;
            //    RaisePropertyChanged("Fields");
            //}
        }

        public ICollectionView RecordsView { get; set; }

        public FormViewModel ParentFormVM { get; set; }

        public ProjectViewModel ProjectVM { get; private set; }
        #endregion // Properties

        public FormBaseViewModel(DataEntryInstrument form, ProjectViewModel projectVM)
        {
            this.Form = form;
            this.ProjectVM = projectVM;
            //SetParentForm(); // can't guarantee this is doable here
        }

        public void SetParentForm()
        {
            if (Form.ParentForm != null)
            {
                Guid parentFormGuid = this.Form.ParentFormId;
                FormViewModel parentFormVM = ProjectVM.GetFormById(parentFormGuid);
                this.ParentFormVM = parentFormVM;
            }
        }

        public virtual void InitalizeCollectionSynchronization()
        {
            RecordsView = new CollectionViewSource { Source = Form.Records }.View;
            //RecordsView.SortDescriptions.Add(new SortDescription("ForeignKey", ListSortDirection.Descending));
            System.Windows.Data.BindingOperations.EnableCollectionSynchronization(RecordsView, _recordsLock);
            this.RecordsView.CollectionChanged += RecordsView_CollectionChanged;
        }

        void RecordsView_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (object item in e.NewItems)
                {
                    Record record = (item as Record);
                    if (record != null)
                    {
                        // set FKEY
                        if (this.ParentFormVM != null && this.ParentFormVM.RecordsView != null)
                        {
                            record.Initialize(Form, (this.ParentFormVM.RecordsView.CurrentItem as Record).Id);
                        }
                        else
                        {
                            record.Initialize(Form);
                        }
                    }
                }
            }
        }
    }
}
